import {deepClone,findObject, removeObject} from './util';

export default class tags {
  static tagsAdd(baseTags,theTags) {
    let theTags2 = deepClone(theTags);
    let baseTags2 = deepClone(baseTags);
    if(baseTags2 == null) {
      baseTags2 = [];
    }
    if(theTags2 != null) {
      theTags2.forEach(theType=>{
        let baseType = findObject(baseTags2,theType.name,'name');
        if(baseType == null) {
          baseTags2.push(theType);
        } else if(theType.groups != null && theType.groups.length != 0) {
          if(baseType.groups == null) {
            baseType.groups = [];
          }
          theType.groups.forEach(theGroup=>{
            let baseGroup = findObject(baseType.groups,theGroup.name,'name');
            if(baseGroup == null) {
              baseType.groups.push(theGroup);
            } else if(theGroup.tags != null && theGroup.tags.length != 0) {
              if(baseGroup.tags == null) {
                baseGroup.tags = [];
              }
              theGroup.tags.forEach(theTag=>{
                let baseTag = findObject(baseGroup.tags,theTag.name,'name');
                if(baseTag == null) {
                  baseGroup.tags.push(theTag);
                } else if(theTag.flag !== undefined) {
                  baseTag.flag = theTag.flag;
                }
              });
            }
          });
        }
        //第一级加入标签
        if (theType.tags!=null &&theType.tags.length != 0){
          if (baseType!=null) {
            baseType.tags = theType.tags;
          }
        }
      });
    }
    return baseTags2;
  }
  static tagsSub(theTags, baseTags) {
    let theTags2 = deepClone(theTags);
    let baseTags2 = deepClone(baseTags);
    if(theTags2 == null) {
      return [];
    }
    if(baseTags2 != null) {
      baseTags2.forEach(baseType=>{
        let theType = findObject(theTags2,baseType.name,'name');
        if(theType != null && theType.groups != null && theType.groups.length != 0 && baseType.groups != null && baseType.groups.length != 0) {
          baseType.groups.forEach(baseGroup=>{
            let theGroup = findObject(theType.groups,baseGroup.name,'name');
            if(theGroup != null && theGroup.tags != null && theGroup.tags.length != 0 && baseGroup.tags != null && baseGroup.tags.length != 0) {
              baseGroup.tags.forEach(baseTag=>{
                let theTag = findObject(theGroup.tags,baseTag.name,'name');
                if(theTag != null) {
                  if(theTag.flag == baseTag.flag) {
                    theGroup.tags.splice(theGroup.tags.indexOf(theTag),1);
                  }
                }
              });
              if(theGroup.tags.length == 0) {
                if(theGroup.type == baseGroup.type) {
                  theType.groups.splice(theType.groups.indexOf(theGroup),1);
                }
              }
            }
          });
          if(theType.groups.length == 0) {
            if(theType.type == baseType.type) {
              theTags2.splice(theTags2.indexOf(theType),1);
            }
          }
        }
      });
    }
    return theTags2;
  }
}
